//
//  YMHomeChooseYearSheet.m
//  YMMedical
//
//  Created by y s on 2023/7/20.
//

#import "YMHomeChooseYearSheet.h"
#import "NSDate+Category.h"

@interface YMHomeChooseYearSheet ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *vPicker;

@property (nonatomic, strong) NSMutableArray *arrYear;
@property (nonatomic, strong) NSMutableArray *arrMonth;
@property (nonatomic, strong) NSMutableArray *arrPreMonth;

@property (nonatomic, copy) NSString *strYear;
@property (nonatomic, copy) NSString *strMonth;
@property (nonatomic, assign) NSInteger thisYear;

@end

@implementation YMHomeChooseYearSheet

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    self.thisYear = [[NSDate date] getDateComponents].year;
    self.strYear = [NSString stringWithFormat:@"%ld",self.thisYear];
    self.strMonth = [NSString stringWithFormat:@"%ld",[[NSDate date] getDateComponents].month];
    
    [self.vBg addSubview:self.vPicker];
    [self.vPicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT);
    }];
    WEAK_SELF;
    self.sureClickBlock = ^{
        if (weakSelf.sureDateBlock) {
            NSString *monthS = weakSelf.strMonth;
            if (weakSelf.strMonth.length < 2){
                monthS = [NSString stringWithFormat:@"0%@",weakSelf.strMonth];
            }
            weakSelf.sureDateBlock([NSString stringWithFormat:@"%@%@%@%@",weakSelf.strYear,KLanguage(@"year_txt"),monthS,KLanguage(@"month_txt")]);
        }
    };
}

#pragma mark -- UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.arrYear.count;
    }else{
        if ([self.strYear integerValue] == self.thisYear){
            return self.arrMonth.count;
        }else{
            return self.arrPreMonth.count;
        }
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *strText = @"";
    if (component == 0 && row < self.arrYear.count) {
        strText = [SDataUtil stringOf:self.arrYear[row]];
        return [NSString stringWithFormat:@"%@%@",strText,KLanguage(@"year_txt")];
    }else if (component == 1){
        if ([self.strYear integerValue] == self.thisYear){
            strText = [SDataUtil stringOf:self.arrMonth[row]];
        }else{
            strText = [SDataUtil stringOf:self.arrPreMonth[row]];
        }
        return [NSString stringWithFormat:@"%@%@",strText,KLanguage(@"month_txt")];
    }
    return @"";
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return SCREEN_WIDTH*0.5;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return [SFitUtil fit:42];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0 && row < self.arrYear.count) {
        self.strYear = [SDataUtil stringOf:self.arrYear[row]];
        [pickerView reloadComponent:1];
    }else if (component == 1){
        if ([self.strYear integerValue] == self.thisYear){
            self.strMonth = [SDataUtil stringOf:self.arrMonth[row]];
        }else{
            self.strMonth = [SDataUtil stringOf:self.arrPreMonth[row]];
        }
    }
}

#pragma mark -- setter and getter
- (UIPickerView *)vPicker{
    if (_vPicker == nil) {
        _vPicker = [[UIPickerView alloc] init];
        _vPicker.delegate = self;
        _vPicker.dataSource = self;
    }
    return _vPicker;
}
- (NSMutableArray *)arrYear{
    if (!_arrYear) {
        _arrYear = [NSMutableArray array];
        NSInteger thisYear = [[NSDate date] getDateComponents].year;
        for (int i = 0; i < 50; i++) {
            [_arrYear addObject:@(thisYear-i)];
        }
    }
    return _arrYear;
}
- (NSMutableArray *)arrMonth{
    if (!_arrMonth) {
        _arrMonth = [NSMutableArray array];
        NSInteger thisMonth = [[NSDate date] getDateComponents].month;
        NSInteger mounthIndex = thisMonth;
        for (int i = 0; i < thisMonth; i ++) {
            mounthIndex = thisMonth - i;
            [_arrMonth addObject:@(mounthIndex)];
        }
    }
    return _arrMonth;
}
- (NSMutableArray *)arrPreMonth{
    if (!_arrPreMonth) {
        _arrPreMonth = [[NSMutableArray alloc] init];
        NSInteger month = 12;
        for (int i = 0; i < 12; i++) {
            month = 12-i;
            [_arrPreMonth addObject:@(month)];
        }
    }
    return _arrPreMonth;
}

@end
